'use strict'
import Storage from '@/common/storage'
import {
  asyncDevice
} from './systeminfo'

// 为了解决setItemEvent不立即生效，使用事件派发强制更高
Storage.dispatchLocalStorageEvent()
Storage.dispatchSessionStorageEvent()

// 全局设备注册授权
let systemInfo = Storage.get('systemInfo')
if (!systemInfo) {
  asyncDevice().then((res) => {
    Storage.set('systemInfo', res)
  })
}

// window全局监听localStorage的setItem事件以及时更新
window.addEventListener('setSessionItemEvent', function (e) {
  console.log('// ---- Set SessionItem Event:', e.newValue)
})
window.addEventListener('setLocalItemEvent', function (e) {
  console.log('// ---- Set LocalItem Event:', e.newValue)
})

const install = Vue => {
  if (install.installed) return
  install.installed = true
  Object.defineProperties(Vue.prototype, {
    $storage: {
      get () {
        return Storage
      }
    }
  })
}

export default install
